package model

import (
	"fmt"
	"gorm.io/gorm"
)

type RpcServer struct {
	gorm.Model
	ModelName   string `gorm:"type:varchar(128);not null"`
	ServiceAddr string `gorm:"type:varchar(255);not null"`
}

func (RpcServer) TableName() string {
	return "rpc_servers"
}

func ListRpcServerByModelName(db *gorm.DB, modelName string) ([]*RpcServer, error) {
	var rpcServers []*RpcServer
	err := db.Where("model_name = ?", modelName).Find(&rpcServers).Error
	if err != nil {
		return nil, err
	}
	if len(rpcServers) == 0 {
		return nil, fmt.Errorf("no available rpc servers found")
	}
	return rpcServers, nil
}
